package com.ateam.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ateam.biz.MissionsBiz;
import com.ateam.biz.SharesBiz;
import com.ateam.model.ChartModel;
import com.ateam.pojo.Companies;
import com.ateam.pojo.Missions;
import com.ateam.pojo.Shares;
import com.ateam.pojo.Users;
import com.ateam.util.DateFactory;
import com.ateam.util.PojoFactory;
import com.ateam.util.SplitFactory;

/**
 * @author WuYuan
 * 
 */
@Controller
public class MissionsController {
	@Resource(name = "missionsBiz")
	private MissionsBiz missionsBiz;

	@Resource(name = "sharesBiz")
	private SharesBiz sharesBiz;

	// 公司层面添加任务
	@ResponseBody
	@RequestMapping("addmission.do")
	public boolean addmission(String missionname, String leaderid, String memberid, String missiondescription,
			String starttime, String endtime, HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				int[] memberids = SplitFactory.getIntarray(memberid);// 切割参与者id
				Missions mission = PojoFactory.getMission();// 获取一个mission
				mission.setMissionname(missionname);
				mission.setDescription(missiondescription);
				mission.setStarttime(DateFactory.getStrToDate(starttime));
				mission.setEndtime(DateFactory.getStrToDate(endtime));

				if (0 != missionsBiz.addMission(mission, Integer.parseInt(leaderid), memberids, 1, 1)) {
					Users user = (Users) session.getAttribute("currentUser");
					Shares share = PojoFactory.getShare();
					share.setContent("发布了任务:" + missionname + "<h5>" + starttime + "</h5> 到 <h5>" + endtime
							+ "</h5>之间  " + missiondescription);
					sharesBiz.addShareByUser(share, user, 1, 1);
					// 如果任务发布成功，那么增加一条share描述这个过程
					return true;
				} else
					return false;
			} else
				return false;
		} else
			return false;
	}

	// 获取用户未完成的任务
	@ResponseBody
	@RequestMapping("getMissionsByUser.do")
	public List<Missions> getMissionsByUser(HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentCompany") != null) {
			if (session.getAttribute("currentUser") != null) {
				Users user = (Users) session.getAttribute("currentUser");
				return missionsBiz.getMissionByUserid(user.getUserid());
			} else
				return null;
		} else
			return null;
	}

	// 完成任务
	@ResponseBody
	@RequestMapping("completemission.do")
	public boolean missionComplete(String missionid, HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentCompany") != null) {
			if (session.getAttribute("currentUser") != null) {
				Users user = (Users) session.getAttribute("currentUser");
				if (0 != missionsBiz.updateMissionComplete(user.getUserid(), Integer.parseInt(missionid))) {
					Missions mission = missionsBiz.getMissionByMissionid(Integer.parseInt(missionid));
					Shares share = PojoFactory.getShare();
					share.setContent("于" + DateFactory.getDateToStr(new Date()) + " 完成了任务<h5>"
							+ mission.getMissionname() + "</h5>: " + mission.getDescription());
					sharesBiz.addShareByUser(share, user, mission.getGroupid(), mission.getProjectid());
					return true;
				} else
					return false;
			} else
				return false;
		} else
			return false;
	}

	// 项目层面上添加任务
	@ResponseBody
	@RequestMapping("projectaddmission.do")
	public boolean projectaddmission(String missionname, String projectid, String leaderid, String memberid,
			String missiondescription, String starttime, String endtime, HttpSession session,
			HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				System.out.println("projectaddmission中的projectid是" + projectid);
				int[] memberids = SplitFactory.getIntarray(memberid);// 切割参与者id
				Missions mission = PojoFactory.getMission();// 获取一个mission
				mission.setMissionname(missionname);
				mission.setDescription(missiondescription);
				mission.setStarttime(DateFactory.getStrToDate(starttime));
				mission.setEndtime(DateFactory.getStrToDate(endtime));

				if (0 != missionsBiz.addMission(mission, Integer.parseInt(leaderid), memberids, 1,
						Integer.parseInt(projectid))) {

					Users user = (Users) session.getAttribute("currentUser");
					Shares share = PojoFactory.getShare();
					share.setContent("发布了任务:" + missionname + "<h5>" + starttime + "</h5> 到 <h5>" + endtime
							+ "</h5>之间  " + missiondescription);
					sharesBiz.addShareByUser(share, user, 1, Integer.parseInt(projectid));
					// 如果任务发布成功，那么增加一条share描述这个过程
					return true;
				} else
					return false;
			} else
				return false;
		} else
			return false;
	}

	// 小组层面上添加任务
	@ResponseBody
	@RequestMapping("groupaddmission.do")
	public boolean groupaddmission(String missionname, String groupid, String leaderid, String memberid,
			String missiondescription, String starttime, String endtime, HttpSession session,
			HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {

				System.out.println("projectaddmission中的groupid是" + groupid);
				int[] memberids = SplitFactory.getIntarray(memberid);// 切割参与者id
				Missions mission = PojoFactory.getMission();// 获取一个mission
				mission.setMissionname(missionname);
				mission.setDescription(missiondescription);
				mission.setStarttime(DateFactory.getStrToDate(starttime));
				mission.setEndtime(DateFactory.getStrToDate(endtime));

				if (0 != missionsBiz.addMission(mission, Integer.parseInt(leaderid), memberids,
						Integer.parseInt(groupid), 1)) {
					if (session.getAttribute("currentUser") != null) {
						Users user = (Users) session.getAttribute("currentUser");
						Shares share = PojoFactory.getShare();
						share.setContent("发布了任务:" + missionname + "<h5>" + starttime + "</h5> 到 <h5>" + endtime
								+ "</h5>之间  " + missiondescription);
						sharesBiz.addShareByUser(share, user, Integer.parseInt(groupid), 1);
						// 如果任务发布成功，那么增加一条share描述这个过程
						return true;
					} else
						return false;
				} else
					return false;
			} else
				return false;
		} else
			return false;
	}

	// 查看任务报表
	@RequestMapping("gotochart.do")
	public String gotochart(HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Companies currentCompany = (Companies) session.getAttribute("currentCompany");
				ChartModel chart = missionsBiz.getChartInfo(currentCompany.getCompanyid());
				session.setAttribute("chart", chart);
				return "chart";
			} else
				return "home";
		} else
			return "index";
	}

	// 查看用户已完成的任务
	@ResponseBody
	@RequestMapping("getMissionsDoneByUser.do")
	public List<Missions> getMissionsDoneByUser(HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Users user = (Users) session.getAttribute("currentUser");
				return missionsBiz.getMissionDoneByUserid(user.getUserid());
			} else
				return null;
		} else
			return null;
	}
}
